package com.cmccgz.udacs_landlord_webapp.main.housemanager.function;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.BuildingCountAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.BuildingCountBean;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;

import java.util.List;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/2 16:51
 */

public class HouseRoomCountFragment extends BaseFragment implements HouseFunctionContract.RoomCountView,View.OnClickListener{

    private TextView tv_building_name;
    private TextView tv_building_count;
    private RecyclerView rv_list;
    private String building_name;
    private String building_id;
    private BuildingCountAdapter adapter;
    private HouseFunctionContract.RoomCountPresenter mPresenter;

    public static HouseRoomCountFragment newInstance(){
        return new HouseRoomCountFragment();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_house_room_count;
    }

    @Override
    protected void initView() {
        initData();
        initTitle();
        findView();
        initRecyclerView();
        showLoadingDialog("获取楼栋统计数据，请稍候...");
        mPresenter.getData(building_id);
    }

    private void initRecyclerView() {
        adapter = new BuildingCountAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_list.setLayoutManager(layoutManager);
        rv_list.setAdapter(adapter);
        rv_list.addItemDecoration(new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL));
    }

    private void findView() {
        tv_building_name = (TextView) findViewById(R.id.tv_building_name);
        tv_building_count = (TextView) findViewById(R.id.tv_building_count);
        rv_list = (RecyclerView) findViewById(R.id.rv_list);
        tv_building_name.setText(building_name);
    }

    private void initData() {
        Bundle bundle = getArguments();
        building_name = bundle.getString(HouseFunctionActivity.BUILDING_NAME);
        building_id = bundle.getString(HouseFunctionActivity.BUILDING_ID);
    }

    private void initTitle() {
        ViewHelper mViewHelper = ViewHelper.instance(this);
        if (mViewHelper !=null) {
            mViewHelper.setTitle("出租统计");
            mViewHelper.setAllVisable(false);
            mViewHelper.setBackTextVisable(true);
        }
    }

    @Override
    public void setPresenter(HouseFunctionContract.RoomCountPresenter presenter) {
        mPresenter = presenter;
    }


    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void getDataSuccess(List<BuildingCountBean> list) {
        dismissLoadingDialog();
        adapter.notifyData(list);
        int size = list.size();
        tv_building_count.setText("共查询到"+size+"条数据");
    }

    @Override
    public void getDataFailed(String errorMsg) {
        dismissLoadingDialog();
        showToast(errorMsg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                mContext.finish();
                break;
        }
    }
}
